package pacman.state;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

public class Menu extends BasicGameState {
    public static final int ID = 0;
    private StateBasedGame sbg;
    private GameContainer gc;
    private AngelCodeFont font;
    private String[] options = new String[] { "Start", "Scoretable", "Quit" };
    private Image logo;
    private int selected;
    private FadeOutTransition fot;
    private FadeInTransition fit;

    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
    	this.gc = gc;
    	this.sbg = sbg;
    	fot = new FadeOutTransition(Color.black);
    	fit = new FadeInTransition(Color.black);
    	try {
    		font = new AngelCodeFont("data/demo2.fnt", "data/demo2_00.tga");
    		logo = new Image("data/logo.gif");
    	} catch (Exception e) {
	    	System.out.println(e.getMessage());
	    }
    }

    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
		g.drawString( "Arrow - movement, ENTER - select options, F2 toggle fullscreen", 20, 580);
		g.drawString("http://code.google.com/p/jpacman/", 520, 580);
		g.setFont(font);
		g.drawImage(logo, 320, 50);
	
		for (int i = 0; i < options.length; i++) {
		    g.drawString(options[i], 400 - (font.getWidth(options[i]) / 2), 200 + (i * 50));
		    if (selected == i) {
		    	g.drawRect(200, 190 + (i * 50), 400, 50);
		    }
		}
    }

    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
    	
    }

    public void keyReleased(int key, char c) {
		if (key == Input.KEY_DOWN) {
		    selected++;
		    if (selected >= options.length) selected = 0;
		}
		if (key == Input.KEY_UP) {
		    selected--;
		    if (selected < 0) selected = options.length - 1;
		}
		if (key == Input.KEY_ENTER) {
		    if (selected == 0) sbg.enterState(Game.ID, fot, fit);
		    if (selected == 1) sbg.enterState(ScoreTable.ID, fot, fit);
		    if (selected == 2) sbg.getContainer().exit();
		}
		if (key == Input.KEY_F2) {
		    try {
			if (!gc.isFullscreen()) {
			    gc.setFullscreen(true);
			} else {
			    gc.setFullscreen(false);
			}
		    } catch (Exception e) {
		    	System.out.println(e.getMessage());
		    }
		}
    }
    @Override
    public int getID() {
    	return ID;
    }
}
